﻿$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetMouldList" },
        dataType: "json",
        url: "../../Ajax/ChanceMouldAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                $("tbody").empty();
                var json = data.json;
                var tbody = "";
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                    tbody += v.MouldName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblSortNum' >";
                    tbody += v.SortNum == "" ? 0 : v.SortNum;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblTypeName' >";
                    tbody += v.TypeName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsTaskCondition' id='" + v.IsTaskCondition + "' >";
                    tbody += v.IsTaskConditionName;
                    tbody += "</label></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").append(tbody)
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    $("#btnAdd").click(function () {
        Edit(null);
    })
})

function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    Edit(lbl);
}

function Delete(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: $(lbl).attr("id") },
            async: true,
            url: "../../Ajax/ChanceMouldAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(lbl).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}

function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var sortNum = $(lbl).parent().parent().find("lable[name=lblSortNum]").first();
    var typeName = $(lbl).parent().parent().find("lable[name=lblTypeName]").first();
    var isTaskCondition = $(lbl).parent().parent().find("lable[name=lblIsTaskCondition]").attr("id");
    var content = "";
    if (lbl != null) {
        content += "</br><table><tr><td>模板名称：<input type='text' id='txtName' value='" + $(lbl).text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>排序号：<input type='text' id='txtSortNum' value='" + $(sortNum).text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>对接名称：<input type='text' id='txtTypeName' value='" + $(typeName).text() + "' style='width:200px;float:right' /></td></tr>"
        if (isTaskCondition == "True")
            content += "<tr><td>是否任务条件：<input type='radio' name='radioIsTaskCondition' id='radioisTure' value='True' checked='checked' /><lable>是</lable><input type='radio' name='radioIsTaskCondition' id='radioisfalse' value='False'/><lable>否</lable></td></tr>";
        else
            content += "<tr><td>是否任务条件：<input type='radio' name='radioIsTaskCondition' id='radioisTure' value='True' /><lable>是</lable><input type='radio' name='radioIsTaskCondition' id='radioisfalse' value='False' checked='checked'/><lable>否</lable></td></tr>";
        content += "</table></br>";
    }
    else {
        content += "<table><tr style='width:290px'><td>模板名称：<input type='text' id='txtName' style='width:200px;float:right' /></td></tr>"
        content += "<tr style='width:290px'><td>排序号：<input type='text' id='txtSortNum' value='' style='width:200px;float:right' /></td></tr>"
        content += "<tr style='width:290px'><td>对接名称：<input type='text' id='txtTypeName' value='' style='width:200px;float:right' /></td></tr>"
        content += "<tr style='width:290px'><td>是否任务条件：<input type='radio' name='radioIsTaskCondition' id='radioisTure' value='True' /><lable>是</lable><input type='radio' name='radioIsTaskCondition' id='radioisfalse' value='False' checked='checked' /><lable>否</lable></td></tr>";
        content += "</table>";
    }
    $.dialog({
        title: "更新概率模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var objectName = $("#txtName").val();
                            if ($.trim(objectName) == "") {
                                $.dialog.alert("请输入模板名称！");
                                return false;
                            }
                            if ($.trim($("#txtTypeName").val()) == "") {
                                $.dialog.alert("请输入对接名称！");
                                return false;
                            }
                            AddMould(objectName, lbl, $(txtSortNum));
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function AddMould(objectName, contr, sortNum) {
    var id = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    var typeName = $(sortNum).parent().parent().parent().find("#txtTypeName").val();
    var isTaskCondition = $(sortNum).parent().parent().parent().find("input[name=radioIsTaskCondition]:checked").val();
    var isTaskConditionName = $(sortNum).parent().parent().parent().find("input[name=radioIsTaskCondition]:checked").text();
    var numVal = $(sortNum).val()
    numVal = numVal == "" ? 0 : numVal;
    var data = contr == null ? { action: "Add", name: objectName, sortNum: numVal, typeName: typeName, isTaskCondition: isTaskCondition} :
              { action: "Update", id: id, name: objectName, sortNum: numVal, typeName: typeName, isTaskCondition: isTaskCondition };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/ChanceMouldAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                $.dialog.tips("更新成功！", 1);
                if (contr == null) {
                    var tbody = "<tr>";
                    tbody += "<td><lable name='lblName' id='" + data + "'>";
                    tbody += objectName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblSortNum' >";
                    tbody += numVal;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblTypeName' >";
                    tbody += typeName;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsTaskCondition' id='" + isTaskCondition + "' >";
                    tbody += isTaskConditionName;
                    tbody += "</label></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                    $("#tb1").append(tbody);
                }
                else {
                    $(contr).text(objectName);
                    $(contr).parent().parent().find("lable[name=lblSortNum]").first().text(numVal);
                    $(contr).parent().parent().find("lable[name=lblTypeName]").first().text(typeName);
                    $(contr).parent().parent().find("lable[name=lblIsTaskCondition]").first().text(isTaskCondition);
                }
            }
            else {
                $.dialog.tips("更新失败！", 1);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}

